<?php

/**
 * Table/InstructionPaieTable.php
 */

namespace RHX\Model\Table;

use Zend\Db\Adapter\Adapter;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use RHX\Model\Object\InstructionPaie;

class InstructionPaieTable extends AbstractTableGateway {

    protected $table = 'rh_instruction_paie';

    /**
     * Constructeur
     * 
     * @param \Zend\Db\Adapter\Adapter $adapter
     */
    public function __construct(Adapter $adapter) {
	$this->adapter			 = $adapter;
	$this->resultSetPrototype	 = new ResultSet();
	$this->resultSetPrototype->setArrayObjectPrototype(new InstructionPaie($adapter));
	$this->initialize();
    }

    /**
     * Récupérer tous les instructions de paie
     *
     * @param type $order
     * @return ResultSet
     */
    public function getAll($order = 'id ASC') {
	return $this->select(function(Select $select)use($order) {
			    $select->order($order);
			});
    }

    /**
     * Récupérer une instruction de paie par sa clé primaire
     *
     * @param type $id
     * @return null|InstructionPaie
     */
    public function get($id) {
	$row = $this->select(array('id' => $id))
		->current();
	if (!$row) {
	    return null;
	}

	return $row;
    }

    /**
     * Récupérer une Instruction de paie pour un employe
     *
     * @param int $matricule
     * @param int $group
     * @return null|InstructionPaie
     */
    public function getOneByMatricule($matricule, $group) {
	$row = $this->select(function(\Zend\Db\Sql\Select $select)use($matricule, $group) {
			    $where = new \Zend\Db\Sql\Where();
			    $where->equalTo('matricule', $matricule);
			    $where->equalTo('group', $group);
			    $select->where($where);
			})
		->current();
	if (!$row) {
	    return null;
	}

	return $row;
    }

    /**
     * Récupérer les Instructions de paie de plusieurs employé
     * 
     * @param array $matriculeArray
     * @param int $group
     * @return ResultSet
     */
    public function getInstructionsByEmployeList($matriculeArray, $group) {
	$rows = $this->select(function(Select $select)use($matriculeArray, $group) {
		    $where = new Where();
		    $where->in('matricule', $matriculeArray);
		    $where->equalTo('group', $group);
		    $select->where($where);
		});

	return $rows;
    }
    
    /**
     * Récupérer les Instructions de paie d'un groupe d'instruction
     * 
     * @param int $group
     * @return array
     */
    public function getInstructionsByGroup($group){
	$rows = $this->select(function(Select $select)use($group){
	    $where = new Where();
	    $where->equalTo('group', $group);
	    $select->where($where);
	});
	$instructions = array();
	foreach ($rows as $row){
	    $instructions[] = $row;
	}
	return $instructions;
    }

    /**
     * Sauvegarde
     *
     * @param \RHX\Model\Object\InstructionPaie $instruction
     * @return int
     */
    public function save(InstructionPaie $instruction) {
	$data	 = array(
	    'group'				 => $instruction->group,
	    'matricule'			 => $instruction->matricule,
	    'anciennete'			 => $instruction->anciennete,
	    'heure_comptabilise'		 => $instruction->heure_comptabilise,
	    'heure_comptabilise'		 => $instruction->heure_comptabilise,
	    'heure_supplementaire'		 => $instruction->heure_supplementaire,
	    'ferie'				 => $instruction->ferie,
	    'heure_positive_libelle'	 => $instruction->heure_positive_libelle,
	    'dimanches'			 => $instruction->dimanches,
	    'heure_negative'		 => $instruction->heure_negative,
	    'heure_negative_libelle'	 => $instruction->heure_negative_libelle,
	    'am1_jour'			 => $instruction->am1_jour,
	    'am1_heure'			 => $instruction->am1_heure,
	    'heure_module_m1'		 => $instruction->heure_module_m1,
	    'heure_module_css'		 => $instruction->heure_module_css,
	    'css_jour'			 => $instruction->css_jour,
	    'css_libelle'			 => $instruction->css_libelle,
	    'laboratoire_heure'		 => $instruction->laboratoire_heure,
	    'entree_sortie_heure'		 => $instruction->entree_sortie_heure,
	    'at_heure'			 => $instruction->at_heure,
	    'cm_heure'			 => $instruction->cm_heure,
	    'am2_heure'			 => $instruction->am2_heure,
	    'maintient_libelle'		 => $instruction->maintient_libelle,
	    'cp_jour'			 => $instruction->cp_jour,
	    'cp_heure'			 => $instruction->cp_heure,
	    'cp_libelle'			 => $instruction->cp_libelle,
	    'prime_montant'			 => $instruction->prime_montant,
	    'prime_libelle'			 => $instruction->prime_libelle,
	    'prime_montant_hors_cp'		 => $instruction->prime_montant_hors_cp,
	    'prime_libelle_hors_cp'		 => $instruction->prime_libelle_hors_cp,
	    'participation'			 => $instruction->participation,
	    'hors_temps_travail_montant'	 => $instruction->hors_temps_travail_montant,
	    'hors_temps_travail_libelle'	 => $instruction->hors_temps_travail_libelle,
	    'repas'				 => $instruction->repas,
	    'mutuelle'			 => $instruction->mutuelle,
	    'vehicule'			 => $instruction->vehicule,
	    'logement'			 => $instruction->logement,
	    'ijss_brute_montant'		 => $instruction->ijss_brute_montant,
	    'ijss_brute_libelle'		 => $instruction->ijss_brute_libelle,
	    'base_co'			 => $instruction->base_co,
	    'ijss_net'			 => $instruction->ijss_net,
	    'acompte'			 => $instruction->acompte,
	    'pret'				 => $instruction->pret,
	    'atd'				 => $instruction->atd,
	    'changement_contrat'		 => $instruction->changement_contrat,
	    'commentaire1'			 => $instruction->commentaire1,
	    'commentaire2'			 => $instruction->commentaire2,
	    'commentaire3'			 => $instruction->commentaire3,
	    'commentaire4'			 => $instruction->commentaire4,
	    'salaire_brute'			 => $instruction->salaire_brute,
	    'total_heure'			 => $instruction->total_heure,
	    'lock'				 => $instruction->lock,
	);
	$id	 = (int) $instruction->id;
	if ($id <= 0) {
	    $this->insert($data);

	    return $this->getLastInsertValue();
	}
	elseif (!is_null($this->get($id))) {
	    $this->update($data, array('id' => $id));

	    return $id;
	}
	else {
	    return 0;
	}
    }

    /**
     * Effacer une Instruction de paie
     * 
     * @param int $id
     * @return int
     */
    public function erase($id) {
	return $this->delete(array('id' => $id));
    }

}

?>
